//
//  UserInfo.m
//  CRM
//
//  Created by Sam on 16/9/28.
//  Copyright © 2016年 AP-EC. All rights reserved.
//

#import "UserInfo.h"

#define KuserName  @"userName"
#define KloginStatus @"loginStatus"
#define Kpassword @"password"
#define Ktoken   @"tolen"
#define KuserNo  @"userNo"
#define KorgName @"orgName" //公司
#define Kname  @"name"//姓名
#define Kphone @"phone"//电话


@implementation UserInfo

singleton_implementation(UserInfo) //单利
#pragma mark - ----------------------
#pragma mark - 字典转模型
-(instancetype)userInfoWithDic:(NSDictionary *)dic{
    UserInfo *userInfo = [UserInfo sharedUserInfo];
    [userInfo setValuesForKeysWithDictionary:dic];
    return userInfo;
}
#pragma mark - ----------------------
#pragma mark - 把系统的setValue:forUndefinedKey:方法覆盖
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}
#pragma mark - ----------------------
#pragma mark - 保存用户信息到沙盒
-(void)saveUserInfoToSanbox{
    
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [UserDefaults setObject:self.userName forKey:KuserName];
    [UserDefaults setBool:self.loginStatus forKey:KloginStatus];
    [UserDefaults setObject:self.password forKey:Kpassword];
    [UserDefaults setObject:self.token forKey:Ktoken];
    [UserDefaults setObject:self.userNo forKey:KuserNo];
    [UserDefaults setObject:self.orgName forKey:KorgName];
    [UserDefaults setObject:self.name forKey:Kname];
    [UserDefaults setObject:self.phone forKey:Kphone];

    [UserDefaults synchronize];
    
}
#pragma mark - ----------------------
#pragma mark - 从沙盒加载用户信息
-(void)loadUserInfoFromSanbox{
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.userName = [UserDefaults objectForKey:KuserName];
    self.loginStatus = [UserDefaults boolForKey:KloginStatus];
    self.password = [UserDefaults objectForKey:Kpassword];
    self.token = [UserDefaults objectForKey:Ktoken];
    self.userNo = [UserDefaults objectForKey:KuserNo];
    self.orgName = [UserDefaults objectForKey:KorgName];
    self.name = [UserDefaults objectForKey:Kname];
    self.phone = [UserDefaults objectForKey:Kphone];
}

#pragma mark - 还原初始状态
-(void)setupBegin{
    
    [UserDefaults removeObjectForKey:Kpassword];
    [UserDefaults removeObjectForKey:Ktoken];
    [UserDefaults removeObjectForKey:KuserNo];
    [UserDefaults removeObjectForKey:KorgName];
    [UserDefaults removeObjectForKey:Kname];
    [UserDefaults removeObjectForKey:Kphone];
 
}

#pragma mark - ----------------------
#pragma mark - 登录状态
-(BOOL)loginStatus {
    
    NSString *userID = [UserDefaults objectForKey:Kpassword];
    if (userID.length) {
        return YES;
    }
    return  NO;
}

@end
